//
//  Growl.m
//  Group Context
//
//  Created by Chris Karr on 2/14/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Growl.h"
#import "Group.h"

#define CONTEXT_UPDATED @"Context Updated"

@implementation Growl

- (void) awakeFromNib
{
	users = [[NSMutableDictionary alloc] init];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(report:) name:CONTEXT_REPORT object:nil];
	
	[GrowlApplicationBridge setGrowlDelegate:self];
}

- (void) report:(NSNotification *) theNote
{
	NSMutableDictionary * user = [NSMutableDictionary dictionaryWithDictionary:[theNote userInfo]];
	[user removeObjectForKey:@"date"];
	
	NSString * userName = [user valueForKey:@"name"];
	
	NSDictionary * ourUser = [users valueForKey:userName];
	
	if (![user isEqualToDictionary:ourUser])
	{
		[users setValue:[user copy] forKey:userName];
		
		NSLog (@"new info");

		NSMutableString * desc = [NSMutableString string];
		
		for (NSString * key in [user allKeys])
		{
			if (!([key isEqual:@"name"] || [key isEqual:@"date"] || [key isEqual:@"icon"]))
			{
				if ([desc length] > 0)
					[desc appendString:@"\n"];
					
				[desc appendString:[NSString stringWithFormat:@"%@: %@", key, [user valueForKey:key]]];
			}
		}
	
		[GrowlApplicationBridge	notifyWithTitle:[NSString stringWithFormat:@"Context Update: %@", userName]
			description:desc notificationName:CONTEXT_UPDATED
			iconData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[user valueForKey:@"icon"]]]
			priority:0 isSticky:NO clickContext:nil];	
	}
}

- (NSDictionary *) registrationDictionaryForGrowl
{
	NSArray * notifications = [NSArray arrayWithObject:CONTEXT_UPDATED];
	
	return [NSDictionary dictionaryWithObjectsAndKeys:notifications, GROWL_NOTIFICATIONS_ALL, notifications, GROWL_NOTIFICATIONS_DEFAULT, nil];
}

@end
